package com.hjy.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    // 创建交换机
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange",true,false);
    }
    // 创建队列 order.delay.queue
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        // 队列信息成为死信后路由到哪个exchange
        args.put("x-dead-letter-exchange","order-event-exchange");
        // 成为死信后路由到指定 exchange 的 路由 key 是什么
        args.put("x-dead-letter-routing-key","order.release.order");
        // 成为死信的 ttl 时间, 默认为毫秒
        args.put("x-message-ttl",60000);
        return new Queue("order.delay.queue",true,false,false,null);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue",true,false,false);
    }

    // 绑定交换机和队列
    @Bean
    public Binding bind1() {
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.create.order",null);
    }

    // 绑定交换机和队列
    @Bean
    public Binding bind2() {
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.release.order",null);
    }
}